import logging

import allure
import jsonpath

from utils.send_request import send_jdbc_request

@allure.step("3.HTTP响应断言")
def http_assert(case, res, index=0):
    expected = eval(case["expected"])
    if case["check"]:
        check = eval(case["check"])
        for c, e in zip(check, expected):
            print(c, e)
            result = jsonpath.jsonpath(res.json(), f"$..{c}")[index]
            logging.info(f"3.http响应断言内容：实际结果({result})==预期结果({e})")
            assert result == e


    # if case["check"]:
    #     result = jsonpath.jsonpath(res.json(), case["check"])[index]
    #     logging.info(f"3.http响应断言内容：实际结果({result})==预期结果({case['expected']})")
    #     assert result == case["expected"]
    else:
        for e in expected:
            logging.info(f"3.http响应断言内容：预期结果({e}) in 实际结果({res.text})")
            assert str(e) in res.text

def jdbc_assert(case):
    # print(case["sql_check"], case["sql_expected"])
    if case["sql_check"] and case["sql_expected"]:
        with allure.step("3.JDBC响应断言"):
            result = send_jdbc_request(case["sql_check"])
            logging.info(f"3.jdbc响应断言内容：实际结果({result})==预期结果({case['sql_expected']})")
            assert result == case["sql_expected"]
